package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertMapper;
import com.zzyl.service.AlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description AlertServiceImpl
 * @Author KangChi Wang
 * @Date 2024-09-14
 */
@Service
public class AlertServiceImpl implements AlertService {

    @Autowired
    private AlertMapper alertMapper;


    @Override
    public void createAlertRule(AlertRuleDto alertRuleDto) {
        //类型转换
        AlertRule alertRule = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertMapper.createAlertRule(alertRule);
    }

    @Override
    public PageResponse<AlertRule> getPages(int pageNum, int pageSize, String alertRuleName, String functionName, String productKey) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        Page<AlertRule> page=alertMapper.getPages( alertRuleName, functionName, productKey);
        page.forEach(page1->{
            String functionName1 = page1.getFunctionName();
            String operator = page1.getOperator();
            Float value = page1.getValue();
            Integer duration = page1.getDuration();
            page1.setRules(functionName1  + operator + value + "," +"持续"+ duration+"个周期就报警");
        });
        PageResponse<AlertRule> alertRulePageResponse = new PageResponse<>();
        alertRulePageResponse.setRecords(page.getResult());
        alertRulePageResponse.setTotal(page.getTotal());
        return alertRulePageResponse;
    }

    /**
     * 获取单个警告规则
     * @param id
     * @return {@link AlertRule }
     */
    @Override
    public AlertRule getById(Long id) {
        AlertRule alertRule=alertMapper.getById(id);
        return alertRule;
    }
}
